<template>
	<view class="order u-skeleton">
		<u-navbar title="订单列表" :background="{
			'backgroundColor':'#fff'
		}" :borderBottom="false" class="u-skeleton-rect">
		</u-navbar>
		<u-tabs-swiper  class="u-skeleton-rect" font-size='28' ref="uTabs" active-color='#000000' inactive-color='#000000' :list="selectList" :current="selectIndex" @change="tabsChange" :is-scroll="false" swiperWidth="750"></u-tabs-swiper>
		<swiper :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish" class="diy-swiper u-skeleton-rect">
			<swiper-item class="swiper-item" v-for="(item, index) in orderList" :key="index">
				<diy-scroll @scrolltolower='scrolltolower' @refresherrefresh='refresherrefresh' :status="item.status" :is_display="item.list.length">
					<view class="item" v-for="(v,n) in item.list" :key="n" @click="gotolink(v)">
						<view class="title">
							<view class="t-left">{{v.order_type == 1 ? '普通' : '卡片' }}</view>
							<view class="t-right">
								<text v-if="v.status == 1">待支付</text>
								<text v-if="v.status == 2">待发货</text>
								<text v-if="v.status == 3">待收货</text>
								<text v-if="v.status == 4">已完成</text>
								<text v-if="v.status == -1">已取消</text>
							</view>
						</view>
						<view class="i-content">
							<image :src="$util.img(v.goods.image)" mode=""></image>
							<view class="i-right">
								<view class="title">
									<view class="name">{{v.goods.name}}</view>
									<view class="num">*{{v.num}}</view>
								</view>
								<view class="number">订单编号：{{v.orderno}}</view>
								<view class="time">下单时间：{{v.create_time}}</view>
							</view>
						</view>
						<view class="i-bottom">订单金额：￥{{v.total_price}}</view>
					</view>
				</diy-scroll>
			</swiper-item>
		</swiper>
		<!-- <u-skeleton :loading="loding" :animation="true" bgColor="#FFF"></u-skeleton> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 顶部标题
				titleList: [{
					title: '订单列表'
				}],
				//判断选中
				current: 0,
				// 选中导航
				selectList: [{
					name: '待支付',
					status:1
				}, {
					name: '待发货',
					status:2
				}, {
					name: '待收货',
					status:3
				}, {
					name: '已完成',
					status:4
				},{
					name: '已取消',
					status:-1
				}],
				//选中的索引
				selectIndex: 0,
				//swiper的值
				swiperCurrent:0,
				//没有数据显示
				is_display:false,
				//列表数据
				orderList:[{
					title:'待支付',
					list:[],
					//加载配置
					page:{
						page:1,
						page_size:8,
					},
					//下面展示的文字
					status:'loadmore'
				},{
					title:'已支付',
					list:[],
					//加载配置
					page:{
						page:1,
						page_size:8,
					},
					//下面展示的文字
					status:'loadmore'
				},{
					title:'待发货',
					list:[],
					//加载配置
					page:{
						page:1,
						page_size:8,
					},
					//下面展示的文字
					status:'loadmore'
				}, {
					name: '已完成',
					list:[],
					//加载配置
					page:{
						page:1,
						page_size:8,
					},
					//下面展示的文字
					status:'loadmore'
				},{
					title:'已取消',
					list:[],
					//加载配置
					page:{
						page:1,
						page_size:8,
					},
					//下面展示的文字
					status:'loadmore'
				}],
				//页面预加载
				loding: false,

			}
		},
		onShow() {
			this.$nextTick(()=>{
				this.refresherrefresh();
			})
		},
		methods: {
			//前往订单详情页面
			gotolink(item){
				this.$util.redirectTo('/pages/order/orderDetail',{id:item.id})
			},
			//导航切换
			tabsChange(e) {
				this.swiperCurrent = e;
			},
			//swiper动画事件
			transition(e) {
				let dx = e.detail.dx;
				this.$refs.uTabs.setDx(dx);
			},
			// 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
			// swiper滑动结束，分别设置tabs和swiper的状态
			animationfinish(e) {
				let current = e.detail.current;
				this.$refs.uTabs.setFinishCurrent(current);
				this.swiperCurrent = this.selectIndex = current;
				//如果当前列表为空，从新调用一边订单列表
				if(!this.orderList[this.selectIndex].list.length){
					this.getorderList();
				}
			},
			//列表刷新事件
			refresherrefresh(callback) {
				this.orderList[this.selectIndex].page = {
					page:1,
					page_size:8,
				}
				this.getorderList(callback);
			},
			// 触底事件
			scrolltolower() {
				this.getorderList();
			},
			//获取订单列表
			getorderList(callback){
				this.$http.asyncSendrequest({
					url:'/order/orderList',
					data:{
						status:this.selectList[this.selectIndex].status,
						...this.orderList[this.selectIndex].page
					},
					success:res=>{
						if(res.code == '0') {
							//如果是第一页的话置空
							if(this.orderList[this.selectIndex].page.page == 1) this.orderList[this.selectIndex].list = [];
							if(res.data.list.length){
								//渲染对应列表之中的数据
								this.orderList[this.selectIndex].list = this.orderList[this.selectIndex].list.concat(JSON.parse(JSON.stringify(res.data.list)));
								this.orderList[this.selectIndex].page.page += 1;
								
								if(res.data.list.length < 8){
									//下面加载的文字
									this.orderList[this.selectIndex].status = 'nomore'
								}
								
								this.$forceUpdate();
							} else {
								//下面加载的文字
								this.status = 'nomore'
							}
							
							if(typeof callback == 'function') callback();
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.order {
		.h-center {
			display: flex;
			align-items: center;

			.h-title {
				margin-right: 44rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
			}

			.h-title:nth-child(2n) {
				margin-right: 0;
			}
		}

		.select {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 40rpx 120rpx 28rpx;
			box-sizing: border-box;
			background: #fff;

			.item {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #000000;
			}
		}
		.diy-swiper{
			margin-top: 20rpx;
			height: calc(100vh - 188rpx);
			.item{
				height: 360rpx;
				background: #FFFFFF;
				border-radius: 23rpx;
				margin: 0 30rpx 20rpx;
				padding: 34rpx;
				box-sizing: border-box;
				.title{
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 30rpx;
					.t-left{
						font-size: 26rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #000000;
					}
					.t-right{
						font-size: 26rpx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #FE2B4E;
					}
				}
				.i-content{
					display: flex;
					image{
						width: 180rpx;
						height: 180rpx;
						border-radius:10rpx;
					}
					.i-right{
						margin-left: 30rpx;
						width: calc(100% - 210rpx);
						.title{
							width: 100%;
							display: flex;
							align-items: center;
							margin: 10rpx 0 0 0;
							.name{
								overflow: hidden; //超出的文本隐藏
								text-overflow: ellipsis; //溢出用省略号显示
								white-space: nowrap;  // 默认不换行；
								width: calc(100% - 80rpx);
								font-size: 28rpx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #000000;
							}
							.num{
								font-size: 24rpx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #000000;
							}
						}
						.number{
							width: 100%;
							font-size: 24rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #000000;
							margin-top: 22rpx;
							overflow: hidden; //超出的文本隐藏
							text-overflow: ellipsis; //溢出用省略号显示
							white-space: nowrap;  // 默认不换行；
						}
						.time{
							font-size: 24rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #666666;
							margin-top: 22rpx;
						}
					}
				}
				.i-bottom{
					text-align: right;
					margin-top: 16rpx;
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #010101
				}
			}
		}
	}

	/deep/ .u-navbar-content-title {
		width: 300rpx !important;
		left: 214rpx !important;
	}
</style>